package monster.com.cvh.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.github.chrisbanes.photoview.OnViewTapListener;
import com.github.chrisbanes.photoview.PhotoView;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.bean.ResumeShopBean;
import monster.com.cvh.constant.Constans;
import monster.com.cvh.database.GreenDAOManager;
import monster.com.cvh.database.ResumeInfoBean;
import monster.com.cvh.event.VisitorRefreshEvent;
import monster.com.cvh.gen.ResumeInfoBeanDao;

/**
 * @author: 13580
 * @data: 2018/4/10
 * @function:图片预览Activity
 */
public class PhotoActivity extends PermissionActivity {
    @BindView(R.id.pv_photo_activity_big_pic)
    PhotoView mPhotoView;
    @BindView(R.id.tv_include_top_bar_title)
    TextView mTvTitle;
    @BindView(R.id.tv_include_top_bar_save)
    TextView mTvAction;
    @BindView(R.id.tv_activity_photo_download)
    TextView mFooter;
    @BindView(R.id.rel_include_top_bar_wrapper)
    RelativeLayout mHeader;
    @BindView(R.id.rel_activity_photo_wrapper)
    RelativeLayout mMain;

    private ResumeShopBean.DataBean mBean;
    private boolean isHiden = false;
    private float mFooterBottomMargin;
    private boolean mIsDownloaded = false;
    private ResumeInfoBeanDao mDao;
    private static final String TAG = "PhotoActivity";

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_photo;
    }

    @Override
    protected void initEvent() {
        changeStatusBarColorDark(PhotoActivity.this, R.color.white);
        mTvAction.setVisibility(View.GONE);
        mTvTitle.setText(getResources().getString(R.string.activity_photo_title));
        if (mIsDownloaded) {
            mFooter.setText(getResources().getString(R.string.activity_photo_downloaded));
        }
        if (mBean != null && !mBean.getPreview_image_url().isEmpty()) {
            Glide.with(this)
                    .load(mBean.getPreview_image_url())
                    .placeholder(R.mipmap.pic_loading)
                    .error(R.mipmap.pic_loading_failed)
                    .thumbnail(0.1f)
                    .fitCenter()
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(mPhotoView);
        }
        mPhotoView.setOnViewTapListener(new OnViewTapListener() {
            @Override
            public void onViewTap(View view, float x, float y) {
                updataHeaderAndFooter();
            }
        });
    }


    @Override
    protected void initData() {
        mDao = GreenDAOManager.getInstance().getmDaoSession().getResumeInfoBeanDao();
        Intent intent = getIntent();
        if (intent != null) {
            mBean = (ResumeShopBean.DataBean) intent.getSerializableExtra(Constans.RESUME_INFO_BEAN);
            if (mBean.getId() <= 8) {
                mIsDownloaded = true;
            } else {
                ResumeInfoBean bean = mDao.queryBuilder().where(ResumeInfoBeanDao.Properties.ResumeId.eq(mBean.getId())).unique();
                if (bean != null) {
                    mIsDownloaded = true;
                }
            }
        }

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mFooter.getLayoutParams();
        mFooterBottomMargin = lp.bottomMargin;
        mFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isLogined()){
                    Intent intent = new Intent(PhotoActivity.this, LoginActivity.class);
                    intent.putExtra(Constans.VISITOR,Constans.LOGIN_WITHOUT_OPTION);
                    startActivityForResult(intent,Constans.LOGIN_WITHOUT_OPTION);
                    return;
                }
                if (mIsDownloaded) {
                    Intent showResumeIntent = new Intent(PhotoActivity.this, MainActivity.class);
                    showResumeIntent.putExtra(Constans.PHOTOACTIVITY_REFRESH_RESUME, true);
                    startActivity(showResumeIntent);
                } else if (!mIsDownloaded && mBean != null) {
                    mDao.insertOrReplace(new ResumeInfoBean(mBean.getId(), mBean.isIs_locked(), mBean.getCover_image_url()));
                    mFooter.setText(getResources().getString(R.string.activity_photo_downloaded));
                    mIsDownloaded = true;
                }
            }
        });
    }


    /**
     * 动画展示
     */
    private void updataHeaderAndFooter() {
        if (isHiden) {
            mHeader.animate().translationY(0);
            mFooter.animate().translationY(0);
            mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        } else {
            mHeader.animate().translationY(-mHeader.getMeasuredHeight());
            mFooter.animate().translationY(mFooter.getMeasuredHeight() + mFooterBottomMargin);
            mMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
        isHiden = !isHiden;
    }

    @OnClick(R.id.iv_include_top_bar_back)
    public void onViewClicked() {
        finish();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Constans.LOGIN_WITHOUT_OPTION){
            Log.e(TAG, "onActivityResult: 游客登录了;请求码：" + requestCode + ";结果码：" + resultCode);
            VisitorRefreshEvent.getInstance().onVisitorLogin();
        }
    }
}
